<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$merchid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$this->model('model')->checkauth($merchid);
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
$tabs=array(
	array('op'=>'list','title'=>'商品列表'),
	array('op'=>'pricelist','title'=>'待审价格'),
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');

if($_RQ['op']=='list'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
	}
	$params=array('type <>'=>4);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid']=$merchid;
	$params['onepluginname']='jy_yuncangshop';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
    if(!empty($_RQ['keyword'])){
        $params['title like']='%'.trim($_RQ['keyword']).'%';
        $return['params']['keyword']=$_RQ['keyword'];
    }
	if(isset($_RQ['status'])){
	    if($_RQ['status']==3){
            $params['status'] = array(0,2,3);
        }else {
            $params['status'] = intval($_RQ['status']);
        }
		$return['params']['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate2']=intval($_RQ['pcate']);
		//$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	/*if(!empty($_RQ['page'])){
		if($this->session('pcate')){
			$params['pcate']=$this->session('pcate');
			$return['params']['pcate']=$this->session('pcate');
		}
	}*/
	if(trim($postdata['keyword'])){
		$params['title like']='%'.trim($postdata['keyword']).'%';
		$return['params']['keyword']=trim($postdata['keyword']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','uniacid','api_data','nochecked','costprice','marketprice','originalprice','paixu','title','image','pcate2','ccate2','scate2','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if(!$list && intval($_RQ['pcate'])){
		unset($params['pcate2']);
		$params['pcate']=intval($_RQ['pcate']);
		$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	}
	if($list){
		foreach($list as &$item){
				$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate2']),'title');
				$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate2']),'title');
				$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
				$item['scatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['scate2']),'title');	
				$item['scatetitle']=$item['scatetitle']?$item['scatetitle']:'无';
				$item['url']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
				$item['salenum']=$item['salenum']?$item['salenum']:0;
				if($this->setting['is_good_jinedit'] && $item['api_pluginname']=='jy_merch' && $item['api_id']>0){
					//$item['total']=pdo_getcolumn('jy_weishop_goods',array('id'=>$item['api_id']),'total');
					//pdo_update('jy_weishop_goods',array('total'=>$item['total']),array('id'=>$item['id']));
				}
				$item['image']=tomedia($item['image']);
				if(!$item['pcate2'] && $item['pcate']){
					pdo_update('jy_weishop_goods',array('pcate2'=>$item['pcate'],'ccate2'=>$item['ccate'],'scate2'=>$item['scate']),array('id'=>$item['id'], 'uniacid' => $_SESSION['uniacid']));
				}
				$item['api_data']=json_decode($item['api_data'],true);
				if($item['api_data']['needchangeprice']){
					$item['needchangeprice']=$item['api_data']['needchangeprice']==1?'待审核':'审核不通过';
				}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：云仓采购，且勾选《云仓进货》下单，下面才会追加库存和商品！','edit'=>true,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				//array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('type'=>'img_string','title'=>'商品分类_Goods group',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle'),
				array('title'=>'三级：_Level 3：','name'=>'scatetitle')
				)
			),
			array('type'=>'img_string','title'=>'库存/销量',
			'data'=>array(
				array('title'=>'库存：','name'=>'total'),
				array('title'=>'销量：','name'=>'salenum','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'nochecked','title'=>'审核状态','type'=>'radio','options'=>'nochecked'),
			array('name'=>'saletype','title'=>'商品属性','type'=>'checkbox','options'=>'saletypes'),
			array('name'=>'api_data.changeprice','title'=>'申请价格','type'=>'string'),
			array('name'=>'needchangeprice','title'=>'价格审核状态','type'=>'string'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_merch/manage.good_list/setstatus0/','title'=>'一键下架'),
			array('type'=>'post','url'=>'jy_merch/manage.good_list/setstatus1/','title'=>'一键上架'),
			array('type'=>'post','url'=>'jy_merch/manage.good_list/setstatus2/','title'=>'一键售罄'),
			array('type'=>'post','url'=>'jy_merch/manage.good_list/setstatus3/','title'=>'一键加入回收站'),
			array('type'=>'modal','url'=>'jy_merch/manage.good_list/uppcate/','title'=>'批量修改分类'),
		),'action'=>array(
			array('type'=>'modal','op'=>'setprice','title'=>'改价'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'商品名称'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'商品分类','options'=>'groups'),
		)
	);
	if($this->setting['is_moreprice']){
		$htmls['list']['list'][]=array('type'=>'img_string','title'=>'成本价/S价',
		'data'=>array(
				array('title'=>'成本价：','name'=>'costprice'),
				array('title'=>'原价：','name'=>'originalprice')
			)
		);
	}
	$return['groups']=pdo_getall('jy_weishop_goods_group',array('onepluginname'=>'jy_yuncangshop','uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'isshow'=>1,'parentid'=>0),array('id','title'));
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
	$return['nochecked']=array('审核通过','未审核');
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/goodyun_index');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}

if($_RQ['op']=='pricelist'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
	}
	$params=array('type <>'=>4);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid']=$merchid;
	$params['api_data like']='%needchangeprice%';
	$params['onepluginname']='jy_yuncangshop';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
    if(!empty($_RQ['keyword'])){
        $params['title like']='%'.trim($_RQ['keyword']).'%';
        $return['params']['keyword']=$_RQ['keyword'];
    }
	if(isset($_RQ['status'])){
	    if($_RQ['status']==3){
            $params['status'] = array(0,2,3);
        }else {
            $params['status'] = intval($_RQ['status']);
        }
		$return['params']['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate2']=intval($_RQ['pcate']);
		//$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	/*if(!empty($_RQ['page'])){
		if($this->session('pcate')){
			$params['pcate']=$this->session('pcate');
			$return['params']['pcate']=$this->session('pcate');
		}
	}*/
	if(trim($postdata['keyword'])){
		$params['title like']='%'.trim($postdata['keyword']).'%';
		$return['params']['keyword']=trim($postdata['keyword']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','api_data','uniacid','nochecked','costprice','marketprice','originalprice','paixu','title','image','pcate2','ccate2','scate2','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if(!$list && intval($_RQ['pcate'])){
		unset($params['pcate2']);
		$params['pcate']=intval($_RQ['pcate']);
		$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	}
	if($list){
		foreach($list as &$item){
				$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate2']),'title');
				$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate2']),'title');
				$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
				$item['scatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['scate2']),'title');	
				$item['scatetitle']=$item['scatetitle']?$item['scatetitle']:'无';
				$item['url']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
				$item['api_data']=json_decode($item['api_data'],true);
				$item['needchangeprice']=$item['api_data']['needchangeprice']==1?'待审核':'审核不通过';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：云仓采购，且勾选《云仓进货》下单，下面才会追加库存和商品！','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				//array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('type'=>'img_string','title'=>'商品分类_Goods group',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle'),
				array('title'=>'三级：_Level 3：','name'=>'scatetitle')
				)
			),
			array('type'=>'img_string','title'=>'库存/销量',
			'data'=>array(
				array('title'=>'库存：','name'=>'total'),
				array('title'=>'销量：','name'=>'salenum','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'api_data.changeprice','title'=>'申请价格','type'=>'string'),
			array('name'=>'needchangeprice','title'=>'价格审核状态','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'商品名称'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'商品分类','options'=>'groups'),
		)
	);
	if($this->setting['is_moreprice']){
		$htmls['list']['list'][]=array('type'=>'img_string','title'=>'成本价/S价',
		'data'=>array(
				array('title'=>'成本价：','name'=>'costprice'),
				array('title'=>'原价：','name'=>'originalprice')
			)
		);
	}
	$return['groups']=pdo_getall('jy_weishop_goods_group',array('onepluginname'=>'jy_yuncangshop','uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'isshow'=>1,'parentid'=>0),array('id','title'));
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
	$return['nochecked']=array('审核通过','未审核');
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/goodyun_index');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='edit'){
	$shopset=plugin('jy_weishop')->plugin_setting('shopset');
	$id=intval($_RQ['id']);
	if($id){
		$isread=pdo_count('jy_weishop_order_goods',array('goodsid'=>$id),'id');
		$item=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($this->setting['is_good_jinedit'] && $item['api_pluginname']=='jy_merch' && $item['api_id']>0){
			exi('采集平台的商品不能编辑！','error');
		}
		$item['dispatch']=pdo_get('jy_weishop_goods_dispatch',array('goodsid'=>$id));
		if($item['istime']==1){
			$item['cuxiao']=2;
		}
		elseif($item['isdiscount']==1){
			$item['cuxiao']=1;
		}
		$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
		
		if($specs_list){
			foreach($specs_list as &$row){
				$row['options']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$row['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			}
			$item['specs']['specs']=$specs_list;
		}
		$item['oldspecs']=$item['specs'];
		$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
		if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('id','specid','title'));
					if($op['specid']){
						$i[$op['specid']]=$op;
					}
				}
				$i['num']=$i['stock'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			$item['specs']['tds']=$option_list;
		}
		$item['api_data']=json_decode($item['api_data'],true);
		$item['api_taocans']=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id,'specs'=>''),array('id','thumb','stock','title','price','productprice','costprice','goodsnum'),'','paixu asc');
	}else{
		$this->model('model')->checkgoodsnum($merchid,true);
		$item=array('status'=>1,'total'=>188);
	}
	if(!$item['dispatch']){
		$item['dispatch']=array('fullmoney'=>'');
	}
	$item['noticemid']=$item['noticemid']?:$_SESSION['uid'];
	$return['item']=$item;
	$return['item']['storeids']=json_decode($return['item']['storeids'],true);
	$return['item']['param']=json_decode($return['item']['param'],true);
	$return['item']['images']=$return['item']['images']?json_decode($return['item']['images'],true):array();
	$return['item']['diyfields']=$return['item']['diyfields']?json_decode($return['item']['diyfields'],true):array();
	$return['item']['fields']=$fields;
	$return['item']['diyprice']=json_decode($return['item']['diyprice'],true);
	$return['item']['pcd']=json_decode($return['item']['pcd'],true);
	$return['item']['deductset']=json_decode($return['item']['deductset'],true);
	$return['item']['commissions']=json_decode($return['item']['commissions'],true);
	$return['item']['no_oldprice']=intval($return['item']['api_data']['no_oldprice']);
	$return['item']['no_oldpriceline']=intval($return['item']['api_data']['no_oldpriceline']);
	$return['item']['api_istaocan']=$return['item']['api_data']['istaocan']?:0;
	$return['item']['api_leixings']=$return['item']['api_data']['leixings'];
	$catelevel=$shopset['level']?3:2;
	//$return['pcddata']=cfc('tools')->pcd();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'paixu','title'=>'排序','type'=>'string','help'=>'数字越大，排名越靠前'),
					array('name'=>'title','title'=>'商品名称','type'=>'string'),
					array('name'=>'image','title'=>'商品主图','type'=>'image','help'=>'建议尺寸：宽580px*高580rpx'),
					array('name'=>'video','title'=>'商品视频','type'=>'video','help'=>'默认优选显示视频，如果想关闭，请移步基础设置-其他设置'),
					array('name'=>'images','title'=>'商品展示图','type'=>'images','help'=>'建议尺寸：宽580px*高650rpx'),
					array('name'=>'subtitle','title'=>'副标题','type'=>'text','help'=>'副标题的长度请控制在100字以内'),
					array('name'=>'shorttitle','title'=>'短标题','type'=>'string','help'=>'商品短标题 用于快递打印,以及小型热敏打印机打印'),
					array('type'=>'select'.$catelevel.'_line','name'=>'pcate2','options'=>'pcates','title'=>'商户分类','help'=>'[温馨提示]：显示商户自己的分类。',
							'name2'=>'ccate2','options2'=>'ccates','title2'=>'请选择二级分类',
							'name3'=>'scate2','options3'=>'scates','title3'=>'请选择三级分类',
					),
				    array('name'=>'saletype','title'=>'商品属性','type'=>'checkbox','options'=>'saletype'),
				)
			),
			array(
				'title'=>'详情',
				'list'=>array(
					array('name'=>'isshowsales','title'=>'不显示销量？','type'=>'switch'),
					array('name'=>'isshowtotal','title'=>'是否显示库存','type'=>'switch'),
					array('type'=>'tip','title'=>'[<red>温馨提示</red>]：默认图片宽度是100%；图片之间不回车换行的话，追加在后面的话，图片之间就没有空白线！！'),
					array('name'=>'content','title'=>'详情','type'=>'richtextold','help'=>''),
				)
			),
		)
	);
	$return['xuni_types']=credit_types();
	$return['de_sales']=$this->de_sales;
	//获取分类
	$myshop=pdo_get('jy_merch_user',array('id'=>$merchid));	
	//满足小程序，不能以商品ID为下标
	$newpcates=array_values($return['syspcates']);
	foreach($newpcates as $k=>$gv){
		if($item['pcate']==$gv['id']){
			$return['item']['pcateindex1']=$k;
		}
	}
	$return['pcates']=pdo_getall('jy_weishop_goods_group',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0,'onepluginname OR'=>'onepluginname=\'jy_merch\' or onepluginname=\'\''),array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
		$pv['ccates']=$pv['cid']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
	}
	//满足小程序，不能以商品ID为下标
	$newpcates=array_values($return['pcates']);
	foreach($newpcates as $k=>$gv){
		if($item['pcate2']==$gv['id']){
			$return['item']['pcateindex']=$k;
		}
	}
	$return['mobilecids']=$mobilecids;
	$dispatchtype=array('0'=>'运费模板','1'=>'统一运费','2'=>'默认模板');
	$totalcnf=array('0'=>'拍下减库存','1'=>'付款减库存','2'=>'永不减库存');
	$needfollow=array('0'=>'否','1'=>'是');
	$noticetype=array('1'=>'付款通知','2'=>'买家收货通知');
	$verifytype=array('0'=>'按订单核销','1'=>'按次核销','2'=>'按消费码核销');
	$return['verifygoodslimittype']=array('0'=>'购买后有效','1'=>'指定过期日期');
	$diyformtype=array('0'=>'关闭','1'=>'自定义','2'=>'使用模板');
	$presellsendtimetype=array('0'=>'固定时间','1'=>'购买时间');
	$return['diyformtemp']=pdo_getall('jy_weishop_diyform_temp',array('uniacid'=>$_SESSION['uniacid']));
	//$return['storeids']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['labels']=pdo_getall('jy_weishop_goods_label',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['type']=$this->goods_types;
	$return['saletype']=$this->saletypes;
	$return['dispatchtype']=$dispatchtype;
	$return['totalcnf']=$totalcnf;
	$return['status']=$status;
	$return['needfollow']=$needfollow;
	$return['noticetype']=$noticetype;
	$return['verifytype']=$verifytype;
	$return['diyformtype']=$diyformtype;
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/goodyun_post');
	}else{
		$return['htmls']=$htmls;
	}
	$return['filed_type']=array(array('id'=>1,'type'=>'字符串'),array('id'=>2,'type'=>'数字'),array('id'=>3,'type'=>'图片'),array('id'=>4,'type'=>'单选'),array('id'=>5,'type'=>'多选'),array('id'=>6,'type'=>'文本'),array('id'=>7,'type'=>'地址'));
	$return['types']=array('1'=>'字符串','2'=>'数字','3'=>'图片','4'=>'单选框','5'=>'多选框','6'=>'文本','7'=>'地址');
	$return['no_onebuys']=array('无限制','不可单独购买','下单必买','下单必买且不可单独购买');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();	
	if(!$postdata['title']){
		exi('必须设置商品名称','error');
	}
	if(!$postdata['image']){
		exi('必须设置商品图片','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'video'=>trim($postdata['video']),
		'subtitle'=>trim($postdata['subtitle']),
		'shorttitle'=>trim($postdata['shorttitle']),
		'keywords'=>trim($postdata['keywords']),
		'paixu'=>intval($postdata['paixu']),
		'isshowsales'=>intval($postdata['isshowsales']),
		'isshowtotal'=>intval($postdata['isshowtotal']),
		'content'=>trim($postdata['content']),
		'images'=>ijson_encode($postdata['images']),
	);
	//获取分类
	$data['ccate2']=$postdata['ccate2'];
	$data['pcate2']=$postdata['pcate2'];
	$data['scate2']=$postdata['scate2'];
	$goodsid=intval($postdata['id']);	
	if($goodsid){
		pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('不能手动添加云仓商品！','error');
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>0),array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('下架成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>1),array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('上架成功！');
}
if($_RQ['op']=='setstatus2'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>2),array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('售罄成功！');
}
if($_RQ['op']=='del'){
	$this->model('model')->checkworker(2);	
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if($_SESSION['os']=='mobile'){
		$postdata=get_postdata();
		$del[]=$postdata['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='realdel'){
	$this->model('model')->checkworker(2);
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if($_SESSION['os']=='mobile'){
		$postdata=get_postdata();
		$del[]=$postdata['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='down'){
	$id=$_RQ['id'];
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('下架成功！');
}
if($_RQ['op']=='up'){
	$id=$_RQ['id'];
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('上架成功！');
}
if($_RQ['op']=='setprice' || $_RQ['op']=='addcosprice'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['changeprice']){
			exi('请先设置改价额','error');
		}
		$goods['api_data']=json_decode($goods['api_data'],true);
		$goods['api_data']['changeprice']=$post['changeprice'];
		$goods['api_data']['needchangeprice']=1;
		$data=array(
			'api_data'=>ijson_encode($goods['api_data']),
		);
		pdo_update('jy_weishop_goods',$data,array('merchid'=>$merchid,'id'=>$id));
		exi('设置成功！','','/manage.goodyun_list/');
	}
	$id=$_RQ['id'];
	if(!$id){
		exi('请先选择商品','error');
	}
	$item=pdo_get('jy_weishop_goods',array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$api_data=json_decode($item['api_data'],true);
	$item['price0']=$api_data['price0'];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'改价',
		'edit'=>array(
			array('type'=>'copy','name'=>'title','title'=>'商品名称'),
			array('type'=>'copy','name'=>'price0','title'=>'平台建议零售价'),
			array('type'=>'copy','name'=>'costprice','title'=>'采购成本价'),
			array('type'=>'copy','name'=>'price','title'=>'当前销售价'),
			array('type'=>'string','title'=>'改价额','name'=>'changeprice','help'=>'修改价格后，需平台审核通过才能生效！'),
		),
	);
	exi($return);
}
if($_RQ['op']=='uppcate'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$ids=$post['ids'];
		if(count($ids)==0){
			exi('请先选择商品','error');
		}
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				$update=array('pcate2'=>intval($post['pcate']),'ccate2'=>intval($post['ccate']),'scate2'=>intval($post['scate']));
				pdo_update('jy_weishop_goods',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
		exi('修改成功！！','','list');
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	$return['item']=array('ids'=>$ids);
	$shopset=$this->plugin_setting('shopset');
	$catelevel=$shopset['level']?3:2;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
				array('type'=>'select'.$catelevel.'_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类','help'=>'[温馨提示]：如果场景下，开启了独立商品，此处只显示本场景下的分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'请选择二级分类',
							'name3'=>'scate','options3'=>'scates','title3'=>'请选择三级分类',
				),
				
		)
	);
	//获取分类
	$groupspram=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'parentid'=>0);
	$return['pcates']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
		$pv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
	}
	$return['htmls']=$htmls;
	exi($return);
}